<?php
    header("Access-Control-Allow-Origin: *");
if (file_get_contents('php://input')) {
    $json = file_get_contents('php://input');
    include './config.php';
    $data = json_decode($json);
    $id = $data->head;
    // $id = $data->head;;
    $sql = '';
    if ($id == '100') {
        $sql = "SELECT * FROM item WHERE flag=1";
    }
    //nav
    if ($id == '101') {
        // $sql = "SELECT * FROM book WHERE flg=1 ORDER BY timer DESC";
        $page = $data->page;
        $sql = "SELECT Id,title,book_img,flg,timer FROM book WHERE flg=1 ORDER BY id DESC LIMIT $page,16";
    }
    //文章
    if ($id == '102') {
        $sql = "SELECT * FROM tag WHERE flag=1";
    }
    //tag
    if ($id == '103') {
        $sql = "SELECT id,title,book_img,timer,look_num FROM book WHERE flg=1 ORDER BY timer DESC limit 0, 10";
    }
    //最近更新文章
    if ($id == '104') {
        $sql = "SELECT id,title,book_img,timer,look_num FROM book WHERE flg=1 order BY look_num+0 DESC limit 0, 10";
    }
    //查看最多
    if ($id == '105') {
        $sql = "SELECT * FROM link WHERE flag=1";
    }
    //友情链接
    if ($id == '106') {
        $key = $data->key;
        $sql = "SELECT id,title,book_img,timer,look_num FROM book WHERE flg=1 AND title like '%{$key}%' OR cont like '%{$key}%' OR tag like '%{$key}%'";
    }
    //搜索
    if ($id == '200') {
        $ids = $data->id;
        $per = $ids - 1;
        $next = $ids + 1;
        $sql_1 = " SELECT * FROM book WHERE id='{$per}' AND flg=1";
        $sql_2 = " SELECT * FROM book WHERE id='{$next}' AND flg=1";
    }
    //上一条下一条
    if ($id == '107') {
        $sql = "SELECT * FROM msg WHERE flag=1 AND DATE_SUB(curdate(),INTERVAL 0 DAY) < timer AND  to_days(now()) >= to_days(new_timer)";
    }//获取通知

    if($id == '108'){
        $sql = "SELECT * FROM ad WHERE flag=1 AND type >= 3 AND DATE_SUB(curdate(),INTERVAL 0 DAY) < over_time AND  to_days(now()) >= to_days(start_time)";
    }//获取铂金及以上广告

    if($id =='109'){
        $ids = $data->ids;
        $sql = "UPDATE book SET Top=top+1 WHERE id='{$ids}'";
    }//更新点赞数量

    if($id =='110'){
        $ids = $data->ids;
        $sql = "UPDATE book SET look_num=look_num+1 WHERE id='{$ids}'";
    }//更新浏览数量

    //数据库配置文件

    if ($id == '200') {
        $arr = array();
        //新建一个数组
        $arr['pre'] = array();
        //存放数据
        $arr['next'] = array();
        //存放数据
        $stmt1 = $conn->query($sql_1);
        $stmt1->execute();
        $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
            array_push($arr['pre'], $v1);
            //添加到数组
        }
        $stmt2 = $conn->query($sql_2);
        $stmt2->execute();
        $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt2->fetchAll()) as $k2 => $v2) {
            array_push($arr['next'], $v2);
            //添加到数组
        }
        if ($result1 || $result2) {
            $arr['code'] = '200';
            $arr['msg'] = '获取数据成功';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        } else {
            $arr['code'] = '400';
            $arr['msg'] = '获取数据失败';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    } else {
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        // 设置结果集为关联数组
        $arr = array();
        //新建一个数组
        $arr['data'] = array();
        //存放数据
        if($id == "110" || $id == "109"){
            $arr['data'] = true;
        }else{
            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            // foreach (new RecursiveArrayIterator($stmt->fetchAll()) as $k => $v) {
            //     array_push($arr['data'], $v);
            //     //添加到数组
            // };
            $arr['data'] = $stmt->fetchAll();
        };
        //便利数据库中的数据
        if ($arr['data']) {
            $arr['code'] = '200';
            $arr['msg'] = '获取数据成功';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        } else {
            $arr['code'] = '400';
            $arr['msg'] = '获取数据失败';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    }
    $conn = null;
} else {
    echo "<script>\r\n    window.location.href = '../';\r\n    </script>";
}

